package com.bdqn;

import java.lang.reflect.Field;

/**
 * lzl
 */
public class PersonReflection_8 {
    public static void main(String[] args) {
        try{
            Class clz = Class.forName("com.bdqn.Person");
            Object person = clz.newInstance();

            //获取被private修饰的name属性
            Field name = clz.getDeclaredField("name");
            name.setAccessible(true);

            //使用Person的实例中的name
            System.out.println("赋值前的name:"+name.get(person));

            //为name属性赋值 指定Person实例中的name属性
            name.set(person,"New Person");

            //展示
            System.out.println("赋值后的name:"+name.get(person));

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
